<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_pumk extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_pumk_model', 'UMK');
		$this->load->model('keu_umk_detail_model', 'UMKDetail');
		$this->load->model('combo_model', 'Combo');
		$this->load->model('keu_coa_model', 'Coa');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = $this->session_menu_label;
		$data['data'] = null;
		$data['data']['date_from'] = date('01/m/Y');
		$data['data']['date_to'] = date('t/m/Y');
		
		viewByTemplate('keu_pumk/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->UMK->GetJSON();
	}
	
	function getdetaildata()
	{
		$this->secure_session();
		echo $this->UMK->GetDetailJSON(post('id_umk'));
	}
	
	
	
	
		
	function create()
	{
	
		$data['title'] = "Tambah ".$this->session_menu_label;
		$data['data'] = NULL;
		$data['office_data']		= json_encode($this->Combo->SWOffice());
		$data['valuta_data']		= json_encode($this->Combo->SWValuta());
		$data['cost_center_data']	= json_encode($this->Combo->SWCostCenter());
		$data['jenis_umk_data']		= json_encode($this->UMK->KeuJenisPUMK());
		
		
		$data['mode'] = "create";
		viewByTemplate('keu_pumk/form', $data, 'form');
	}

	function edit()
	{
		$data['title'] = "Ubah ".$this->session_menu_label;
		$data['data'] = $this->UMK->GetSingleView(get('id_umk'));
				
		$data['office_data']		= json_encode($this->Combo->SWOffice());
		$data['valuta_data']		= json_encode($this->Combo->SWValuta());
		$data['cost_center_data']	= json_encode($this->Combo->SWCostCenter());
		$data['jenis_umk_data']		= json_encode($this->UMK->KeuJenisPUMK());
		
		$data['mode'] = "edit";
		viewByTemplate('keu_pumk/form', $data, 'form');
	}

	
	function GetKurs(){
		$arrkurs = $this->UMK->GetKurs(post('kd_valuta'), date_format3(post('tgl_umk'), 'YYYY-MM-DD'));
		echo $arrkurs['harga_rata'];
	}
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		// Validation
		if (post("kd_kantor") == '') {
			$error->Add("kd_kantor", "Kantor", $tpl_empty);
		}
		
		if (post("kd_cost_center") == '') {
			$error->Add("kd_cost_center", "Cost Center", $tpl_empty);
		}
		
		if (post("kd_jenis_umk") == '') {
			$error->Add("kd_jenis_umk", "Jenis UMK", $tpl_empty);
		}
		
		if (post("tgl_umk") == '') {
			$error->Add("tgl_umk", "Tanggal UMK", $tpl_empty);
		}
		
		if (post("kd_valuta") == '') {
			$error->Add("kd_valuta", "Mata Uang", $tpl_empty);
		}
		
		if (post("jml_dana_diajukan") == '') {
			$error->Add("jml_dana_diajukan", "Jumlah Dana", $tpl_empty);
		}
		
		if (post("keterangan") == '') {
			$error->Add("keterangan", "Keterangan", $tpl_empty);
		}
		
		return $error;
	}
	
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('id_umk', 'kd_kantor', 'tgl_umk',  'keterangan',  'kurs', 'kd_valuta', 'kd_cost_center', 'jml_dana_diajukan', 'kd_jenis_umk'));
				
				
				$data['kurs']			= toNumber($data['kurs']);
				
				
				if (post("mode") == "create")
				{
				
					$suffix 	= "/UMK/".date_format3(post('tgl_umk'), 'MM').'/'.date_format3(post('tgl_umk'), 'YYYY');
					$id_umk		= $this->get_data("SELECT (NVL((SELECT MAX(SUBSTR(ID_UMK,0,LENGTH(ID_UMK)-LENGTH('".$suffix."'))) FROM KEU_UMK WHERE SUBSTR(ID_UMK,LENGTH('".$suffix."')*-1,LENGTH('".$suffix."')) = '".$suffix."'),0))+1 CODE FROM DUAL");
					$id_umk		= str_pad($id_umk, 2, '0', STR_PAD_LEFT).$suffix;
					
					$data['id_umk']					= $id_umk;
					$data['status_approve']			= 'P';
					$data['tgl_umk']				= date_format3(post('tgl_umk'), 'YYYY-MM-DD');
					$data['jml_dana_diajukan']		= toNumber(post('jml_dana_diajukan'));
					
					$data['create_date']			= NOW;
					$data['create_user']			= USERID;

					$this->UMK->Add($data);
					/*
					$data_det['id_umk']				= $id_umk;
					$data_det['no_umk']				= $this->get_data('SELECT KEU_UMK_DETAIL_SEQ.NEXTVAL FROM DUAL');
					$data_det['keterangan']			= post('keterangan');
					$data_det['dana_diajukan']		= $data['jml_dana_diajukan'];
					$data_det['create_date']		= NOW;
					$data_det['create_user']		= USERID;

					
					$this->UMKDetail->Add($data_det);
					*/
					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
				
					$data['tgl_umk']			= date_format3(post('tgl_umk'), 'YYYY-MM-DD');
					$data['jml_dana_diajukan']		= toNumber(post('jml_dana_diajukan'));
					$data['modify_date']		= NOW;
					$data['modify_user']		= USERID;
					
					$this->UMK->Update($data, 
										array('id_umk'	=> $data['id_umk']));
					
					/*
					$data_det['keterangan']			= post('keterangan');
					$data_det['dana_diajukan']		= $data['jml_dana_diajukan'];
					$data_det['create_date']		= NOW;
					$data_det['create_user']		= USERID;

					
					$this->UMKDetail->Update($data_det, 
										array('id_umk'	=> $data['id_umk']));
					*/
					
					$ls_response = "Data Tersimpan";
					
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
				
				// Detail Payment
				$dtl_umk		= $this->data['dtl_umk'];
				$total_deleted	= $dtl_umk['num_row']['deleted'];
				$total_inserted	= $dtl_umk['num_row']['inserted'];
				$total_updated	= $dtl_umk['num_row']['updated'];
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_umk['deleted'][$i];
					$this->UMKDetail->DeleteDetailUMK($data['id_umk'], $row['no_umk']);
				}
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					$row = $dtl_umk['inserted'][$i];
					
					$row['jml_unit_diajukan']	= toNumber($row['jml_unit_diajukan'])=='NaN' ? 0 : toNumber($row['jml_unit_diajukan']);
					$row['harga_satuan']		= toNumber($row['harga_satuan'])=='NaN' ? 0 : toNumber($row['harga_satuan']);
					$row['dana_diajukan']		= toNumber($row['dana_diajukan'])=='NaN' ? 0 : toNumber($row['dana_diajukan']);
					$row['id_umk'] 				= $data['id_umk'];
					$row['no_umk']				= $this->get_data('SELECT KEU_UMK_DETAIL_SEQ.NEXTVAL FROM DUAL');
					
					$row['jml_unit_disetujui']	= $row['jml_unit_diajukan'];
					$row['dana_disetujui']		= $row['dana_diajukan'];
					
					
					$row['jml_unit_pertanggungjwb']	= $row['jml_unit_diajukan'];
					$row['dana_pertanggungjwb']		= $row['dana_diajukan'];
					$row['harga_real_satuan']		= $row['harga_satuan'];
					
					$row['create_date']	= NOW;
					$row['create_user']	= USERID;
					
					$this->UMKDetail->Add($row);
				}
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_umk['updated'][$i];
					$row['jml_unit_diajukan']	= toNumber($row['jml_unit_diajukan'])=='NaN' ? 0 : toNumber($row['jml_unit_diajukan']);
					$row['harga_satuan']		= toNumber($row['harga_satuan'])=='NaN' ? 0 : toNumber($row['harga_satuan']);
					$row['dana_diajukan']		= toNumber($row['dana_diajukan'])=='NaN' ? 0 : toNumber($row['dana_diajukan']);
					$row['id_umk'] 				= $data['id_umk'];
					
					
					$row['jml_unit_disetujui']	= $row['jml_unit_diajukan'];
					$row['dana_disetujui']		= $row['dana_diajukan'];
										
					$row['jml_unit_pertanggungjwb']	= $row['jml_unit_diajukan'];
					$row['dana_pertanggungjwb']		= $row['dana_diajukan'];
					$row['harga_real_satuan']		= $row['harga_satuan'];
					
					$row['modify_date']	= NOW;
					$row['modify_user']	= USERID;
				
				
					$this->UMKDetail->Update($row, array(	'id_umk'	=> $row['id_umk'], 
															'no_umk'	=> $row['no_umk'] ));
				}
				
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$id_umk	= get('id_umk');
		
		if ($id_umk != "")
		{
			// Delete Single
			$ls_result = $this->UMK->DeleteSingle($id_umk);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->UMK->DeleteSingle($row['id_umk']);
			}
		}
		
		echo $ls_result;
	}
	
	function dtl_umk_data()
	{
		echo $this->UMKDetail->GetJSON();
	}
	
	function print_umk(){
		$as_id_umk	= get('id_umk');
		
		if($as_id_umk != ''){
			
			$this->load->library('Terbilang');
			$Terbilang = new Terbilang();
			
			// Load FPDF
			$this->load->helper('fpdf');
			
			// Set Data
			$result	= $this->db->query("SELECT	*
										FROM KEU_UMK WHERE ID_UMK = '".$as_id_umk."'");
			
			$row				= $result->row_array();
			$row['terbilang']	= $Terbilang->eja( $row['jml_dana_disetujui'] ).' rupiah';
			$data['data']		= $row;
			
			
			$result	= $this->db->query("SELECT * FROM KEU_UMK_DETAIL WHERE ID_UMK = '".$as_id_umk."' AND NVL(DANA_DISETUJUI, 0)>0");
			if($result->num_rows() >0){
				$data['arr_detail']	= $result->result_array();
			}else{
				$data['arr_detail']	= array();
			}
						
			// Load View
			$this->load->view('keu_pumk/print_umk', $data);
			
		}
	}
	
	
	
	function print_pumk(){
		$as_id_umk	= get('id_umk');
		
		if($as_id_umk != ''){
			
			$this->load->library('Terbilang');
			$Terbilang = new Terbilang();
			
			// Load FPDF
			$this->load->helper('fpdf');
			
			// Set Data
			$result	= $this->db->query("SELECT	*
										FROM KEU_UMK WHERE ID_UMK = '".$as_id_umk."'");
			
			$row				= $result->row_array();
			$row['terbilang']	= $Terbilang->eja( $row['jml_dana_disetujui'] ).' rupiah';
			$data['data']		= $row;
			
			
			$result	= $this->db->query("SELECT * 
										FROM KEU_UMK_DETAIL 
										WHERE ID_UMK = '".$as_id_umk."' AND 
												(NVL(DANA_DISETUJUI, 0)>0 OR NVL(DANA_PERTANGGUNGJWB,0)>0)");
			if($result->num_rows() >0){
				$data['arr_detail']	= $result->result_array();
			}else{
				$data['arr_detail']	= array();
			}
						
			// Load View
			$this->load->view('keu_pumk/print_pumk', $data);
			
		}
	}
	
	
	
	function print_pumk_sispi(){
		$ldt_umk	= get('tgl_pertanggungjawaban');
		
		if($ldt_umk != ''){
			
			$this->load->library('Terbilang');
			$Terbilang = new Terbilang();
			
			// Load FPDF
			$this->load->helper('fpdf');
			
			// Set Data
					
			$result	= $this->db->query("SELECT NULL ID_UMK,
											   'UMK' KETERANGAN_UMK,
											   SUM (CASE WHEN STATUS_DANA = ' 0' THEN -1 ELSE 1 END * JML_DANA)
												  TOTAL_UMK,
											   NULL ID_ANGGOTA,
											   NULL NAMA_ANGGOTA,
											   NULL JML_PINJAMAN
										  FROM KEU_POOLING_PINJAMAN
										 WHERE CASE
												  WHEN TGL_DANA = TO_DATE ('03/11/2013', 'DD/MM/YYYY')
													   AND STATUS_DANA = '0'
												  THEN
													 TGL_DANA + 1
												  ELSE
													 TGL_DANA
											   END <= TO_DATE ('03/11/2013', 'DD/MM/YYYY')
										UNION ALL
										SELECT NULL,
											   PIN.ID_ANGGOTA ||' '|| P.FULL_NAME,
											   NULL,
											   PIN.ID_ANGGOTA,
											   P.FULL_NAME NAMA_ANGGOTA,
											   PIN.JML_PINJAMAN
										  FROM SIS_PINJAMAN_ANGGOTA PIN
											   INNER JOIN SIS_ANGGOTA AG
												  ON AG.ID_ANGGOTA = PIN.ID_ANGGOTA
											   INNER JOIN SW_PERSON P
												  ON AG.PERSON_ID = P.PERSON_ID
										 WHERE ID_PINJAMAN IN
												  (SELECT ID_PINJAMAN
													 FROM KEU_POOLING_PINJAMAN
													WHERE TGL_DANA = TO_DATE ('03/11/2013', 'DD/MM/YYYY'))");
			if($result->num_rows() >0){
				$data['arr_detail']	= $result->result_array();
			}else{
				$data['arr_detail']	= array();
			}
						
			// Load View
			$this->load->view('keu_pumk/print_pumk_sispi', $data);
			
		}
	}
	
}
?>